/*
 *所有关于workstationmanagementmodel类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*workstationmanagementmodelService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using VOL.Core.BaseProvider;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Linq;
using VOL.Core.Utilities;
using System.Linq.Expressions;
using VOL.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.Services.IRepositories;
using VOL.Core.ManageUser;

namespace VOL.Services.Services
{
    public partial class workstationmanagementmodelService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IworkstationmanagementmodelRepository _repository;//访问数据库

        [ActivatorUtilitiesConstructor]
        public workstationmanagementmodelService(
            IworkstationmanagementmodelRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }
        //添加业务逻辑代码
        public override WebResponseContent Add(SaveModel saveDataModel)
        {
            //此处saveModel是从前台提交的原生数据，可对数据进修改过滤
            //saveModel.MainData["字段"]="值";//这里可以对提交的原来参数处理或者添加其他字段
            WebResponseContent webResponse = new WebResponseContent();
            // 在保存数据库前的操作，所有数据都验证通过了，这一步执行完就执行数据库保存
            AddOnExecuting = (workstationmanagementmodel order, object list) =>
            {
                //明细表对象
                List<List<workstationmanagementmodel>> orderLists = list as List<List<workstationmanagementmodel>>;

                //创建时间
                order.CreateTime = DateTime.Now;
                //创建编码，每次自动生成以F开头的唯一编码有顺序
                // order.WorkShopCode = "F" + DateTime.Now.ToString("yyyyMMddHHmmssfff");

                //创建人是登录名称+登录用户名
                order.CreateUserName = (UserContext.Current.UserName) + UserContext.Current.UserTrueName;


                //未删除
                order.IsDeleted = 1;

                return webResponse.OK();
            };

            var res = base.Add(saveDataModel);
            //这里在保存成功后做一些其他操作
            if (res.Status)
            {

            }
            return res;
        }
        /// <summary>
        /// 更新方法
        /// </summary>
        /// <param name="saveModel"></param>
        /// <returns></returns>
        public override WebResponseContent Update(SaveModel saveModel)
        {
            WebResponseContent webResponse = new WebResponseContent(true);
            UpdateOnExecuting = (workstationmanagementmodel order, object addList, object updateList, List<object> delKeys) =>
            {
                //根据id获取数据
                var orderModel = _repository.Find(x => x.WorkStationId == order.WorkStationId).FirstOrDefault();
                if (orderModel != null)
                {
                    order.UpdateTime = DateTime.Now;
                    order.CreateTime = orderModel.CreateTime;
                    order.CreateUserName = orderModel.CreateUserName;
                    order.IsDeleted = 1;
                    order.UpdateUserName = UserContext.Current.UserName + UserContext.Current.UserTrueName;
                }
                ;
                _repository.Update(order);
                return webResponse.OK();
            };

            return base.Update(saveModel);
        }
    }
}
